package cn.dingdan.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.dingdan.pojo.Boss;
import cn.dingdan.pojo.Waiter;
import cn.dingdan.sercice.BossService;
import cn.dingdan.sercice.WaiterService;
import cn.dingdan.tools.Contans;

@Controller
public class LoginController {
	
	@Resource(name="waiterServiceImpl")
	private WaiterService waiterService;
	@Resource(name="bossServiceImpl")
	private BossService bossService;
	
	@RequestMapping(value="login",method=RequestMethod.GET)
	public String loginBoss(){
		return "login";
	}
	
	
	@RequestMapping(value="login.html",method=RequestMethod.POST)
	@ResponseBody
	public Object  toLoginBoss(String code,String password,HttpSession session){
		Map<String,Object> map = new HashMap<String, Object>();
		if("admin".equals(code)){
			Boss boss_01 = new Boss(code,password);
			Boss boss = bossService.getLogin(boss_01);
			map.put("success", boss!=null);
			if (boss!=null) {
				session.setAttribute(Contans.USER_SESSION, boss);
				map.put("url", "/boss");
			}
		}else {
			Waiter waiter =waiterService.getLogin(code,password);
			map.put("success", waiter!=null);
			if (waiter != null) {
				session.setAttribute(Contans.USER_SESSION, waiter);
				map.put("url","/privider");
			}
		}
		return map;
	}
}
